package com.macrosoftinc.feedreaderxtreme.fragments;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.actionbarsherlock.app.SherlockListFragment;
import com.macrosoft.frpd.parser.helper.TorrentFeedBO;
import com.macrosoft.frpd.utils.Logger;
import com.macrosoft.frpd.utils.Logger.DebugLevel;
import com.macrosoftinc.feedreaderxtreme.R;
import com.macrosoftinc.feedreaderxtreme.adapter.CustomTorrentsListAdapter;
import com.macrosoftinc.feedreaderxtreme.widget.RefreshableListView;

public class CategoriesDetailFragment extends SherlockListFragment {

	String mCurrentTab;
	String currentTagUrl;
	int currentTabPosition;

	RefreshableListView listView;
	RelativeLayout torrentItemsProgressDialog;
	List<TorrentFeedBO> items;
	private CustomTorrentsListAdapter mAdapter;
	LinearLayout latestTopLayout;
	boolean latestTop;
	HashMap<String, String> mProperties;

	public static CategoriesDetailFragment newInstance(String tab, String currentTagUrl, int position, HashMap<String, String> latestTopMap) {
		CategoriesDetailFragment f = new CategoriesDetailFragment();
		// Supply num input as an argument.
		Bundle data = new Bundle();
		data.putString("current_tab", tab);
		data.putString("current_tag_url", currentTagUrl);
		// data.putBoolean("latestTop", latestTop);
		data.putSerializable("latestTop", latestTopMap);
		data.putInt("current_tab_position", position);
		Logger.log(DebugLevel.INFO, "Setting Arguments==" + tab);
		f.setArguments(data);
		return f;
	}

	/**
	 * When creating, retrieve this instance's number from its arguments.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle data = getArguments();

		if (data != null) {
			mCurrentTab = data.getString("current_tab");
			mProperties = (HashMap<String, String>) data.getSerializable("latestTop");
			Logger.log(DebugLevel.INFO, "onCreate==-----latestTop" + latestTop);
		}
	}

	/**
	 * The Fragment's UI is just a simple text view showing its instance number.
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Logger.log(DebugLevel.INFO, "OnCreateView==" + mCurrentTab + "-----savedInstanceState" + savedInstanceState);
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_list_with_empty_container, container, false);
		root.setBackgroundColor(Color.WHITE);
		listView = ((RefreshableListView) root.findViewById(android.R.id.list));
		torrentItemsProgressDialog = (RelativeLayout) root.findViewById(R.id.cat_progress_bar_layout);
		latestTopLayout = (LinearLayout) root.findViewById(R.id.latest_top_layout_id);
		// latestTopLayout.findViewById(R.id.latest_torrent_id).setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// Logger.i("//////------latest torrent clicked");
		// }
		// });
		// latestTopLayout.findViewById(R.id.top_torrent_id).setOnClickListener(new OnClickListener() {
		//
		// @Override
		// public void onClick(View v) {
		// // TODO Auto-generated method stub
		// Logger.i("//////------top torrent clicked");
		// }
		// });
		Logger.i("..........................................listview" + listView.getId());
		return root;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Logger.log(DebugLevel.INFO, "onActivityCreated==" + mCurrentTab + "----------listview" + listView);
		// updateTorrentListAdpater();
		// setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings));
		updateTorrentListAdpater(false);
	}

	/**
	 * @param lisAdpater
	 *            the lisAdpater to set
	 */
	public void updateTorrentListAdpater(boolean loadMore) {
		Logger.log(DebugLevel.INFO, "this.listView=" + this.listView);
		if (this.items != null) {
//			Logger.i("updateTorrentListAdpater called this.items.get(0).title===" + this.items.get(0).title);
			if (this.mAdapter == null) {
				this.mAdapter = new CustomTorrentsListAdapter(this.listView.getContext(), R.layout.torrent_detail_list_item, R.id.torrent_title, this.items);
				this.listView.setAdapter(this.mAdapter);
			} else {
				if (loadMore)
					this.mAdapter.getItems().addAll(this.items);
				else {
					this.mAdapter = new CustomTorrentsListAdapter(this.listView.getContext(), R.layout.torrent_detail_list_item, R.id.torrent_title, this.items);
					this.listView.setAdapter(this.mAdapter);
				}
				Logger.i("updateTorrentListAdpater called items.size()===" + this.mAdapter.getItems().size());
				this.mAdapter.notifyDataSetChanged();
			}
		}
	}

	public void setLatestTopLayoutState() {
		if (latestTopLayout != null)
			if (mProperties != null && mProperties.size() > 0) {
				showLatestTopLayout();
				if (mProperties.get("Latest torrents") != null)
					latestTopLayout.findViewById(R.id.latest_torrent_id).setTag(mProperties.get("Latest torrents"));
				if (mProperties.get("Today top torrents") != null)
					latestTopLayout.findViewById(R.id.top_torrent_id).setTag(mProperties.get("Today top torrents"));
			} else {
				hideLatestTopLayout();
			}
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
//		Logger.log(DebugLevel.INFO, "/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*onDestroy==*CategoriesDetailFragment onDestroy==" + mCurrentTab);
	}

	/**
	 * @return the listView
	 */
	public RefreshableListView getListView() {
//		Logger.i(".................................................................listview" + listView.getId());
		return listView;
	}

	/**
	 * @return the mCurrentTab
	 */
	public String getmCurrentTab() {
		return mCurrentTab;
	}

	/**
	 * @param mCurrentTab
	 *            the mCurrentTab to set
	 */
	public void setmCurrentTab(String mCurrentTab) {
		this.mCurrentTab = mCurrentTab;
	}

	/**
	 * @return the currentTabPosition
	 */
	public int getCurrentTabPosition() {
		return currentTabPosition;
	}

	/**
	 * @param currentTabPosition
	 *            the currentTabPosition to set
	 */
	public void setCurrentTabPosition(int currentTabPosition) {
		this.currentTabPosition = currentTabPosition;
	}

	/**
	 * @return the items
	 */
	public List<TorrentFeedBO> getItems() {
		return items;
	}

	/**
	 * @param items
	 *            the items to set
	 */
	public void setItems(List<TorrentFeedBO> items) {
		this.items = items;
	}

	/**
	 * @return the latestTopLayout
	 */
	public LinearLayout getLatestTopLayout() {
		return latestTopLayout;
	}

	/**
	 * @return the mProperties
	 */
	public HashMap<String, String> getmProperties() {
		return mProperties;
	}

	/**
	 * @param mProperties
	 *            the mProperties to set
	 */
	public void setmProperties(HashMap<String, String> mProperties) {
		this.mProperties = mProperties;
	}

	/**
	 * @param latestTopLayout
	 *            the latestTopLayout to set
	 */
	public void setLatestTopLayout(LinearLayout latestTopLayout) {
		this.latestTopLayout = latestTopLayout;
	}

	public void showLatestTopLayout() {
		if (latestTopLayout != null)
			latestTopLayout.setVisibility(LinearLayout.VISIBLE);
	}

	public void hideLatestTopLayout() {
		if (latestTopLayout != null)
			latestTopLayout.setVisibility(LinearLayout.GONE);
	}

	public void showTorrentItemsProgressDialog() {
		if (torrentItemsProgressDialog != null)
			torrentItemsProgressDialog.setVisibility(ProgressBar.VISIBLE);
	}

	public void hideTorrentItemsProgressDialog() {
		if (torrentItemsProgressDialog != null)
			torrentItemsProgressDialog.setVisibility(ProgressBar.GONE);
	}
}
